$("#return_list").click(function(){
	window.location = "../healthFile/getIndexPage";
});

$(".form-horizontal").validationEngine();//验证表单


var submitAllFlag=true;
//健康档案添加页面==基本信息调用
$("#health_base").click(function(){
	var $text1 = $(this).text();
	if($text1 == "保存"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
		save_health_base("修改页面获得查看页面")
	}
	if($text1 == "编辑"){
		get_update_health_base("修改页面获得编辑页面")
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
	}
});
//健康档案查看页面==基本信息调用
$("#see_member_base").click(function(){
	var $text2 = $(this).text();
	if($text2 == "保存"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
		save_health_base("查看页面获得查看页面")
	}
	if($text2 == "编辑"){
		get_update_health_base("查看页面获得编辑页面");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
	}
	//see_healthBase
});
var validateBaseFlag=0;//如果标记为1则不进行重复验证
var validateDrugFlag=0;//如果标记为1则不进行重复验证
var validateExposeFlag=0;//如果标记为1则不进行重复验证
var validateLivingFlag=0;//如果标记为1则不进行重复验证
//保存健康档案基本信息
function save_health_base(flag){
	checkeatState();
	checkSomekeState();
	checkDrinkState();
	var iWegith=$("#body_height").val();
	if(iWegith==''){
		iWegith=0;
	}
	var disabilityState = $("#disabilityState :radio:checked").val();
	var disabilityRemark = $("#disabilityRemark").val();
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../healthbase/save_healthbase",
		async: false,
		data:{
			memberId:$("#memberId").val(),
			bodyHeight:iWegith,
			bodyWeight:$("#body_weight").val(),
			work:$("#work").val(),
			sleep:$("#sleep").val(),
			eat:$("#eat").val(),
			visionLeft:$("#vision_left").val(),
			visionRight:$("#vision_right").val(),
			listeningLeft:$("#listening_left").val(),
			listeningRight:$("#listening_right").val(),
			isSmoke:$("#is_smoke").val(),
			someNum:$("#some_num").val(),
			smokeType:$("#smoke_type").val(),
			somkeYear:$("#somke_year").val(),
			isDrink:$("#is_drink").val(),
			drinkNum:$("#drink_num").val(),
			drinkType:$("#drink_type").val(),
			drinkYear:$("#drink_year").val(),
			exerciseNum:$("#exercise_num").val(),
			exerciseTime:$("#exercise_time").val(),
			exerciseType:$("#exercise_type").val(),
			healthcare:$("#healthcare").val(),
			remark:$("#remark").val(),
			lastUpdateNo:$("#last_update_no").val(),
			serverStationCode:$("#serverStationCode").val(),
			disabilityState:disabilityState,
			disabilityRemark:disabilityRemark
		},
		success : function(data) {
		}
	});
	
}

//获得健康档案基本信息展示页面
function get_see_health_base(flag){
	$.ajax({
		type : "post",
		dataType: "html",
		url : "../healthbase/get_see_healthbase",
		async: true,
		data:{
			memberId:$("#memberId").val()
		},
		success : function(data) {
			$("#see_healthBase").empty().html(data);
			
		}
	});
}
//获得健康档案基本信息编辑页面
function get_update_health_base(flag){
	$.ajax({
		type : "post",
		dataType: "html",
		url : "../healthbase/get_update_healthbase",
		async: true,
		data:{
			memberId:$("#memberId").val()
		},
		success : function(data) {
			$("#see_healthBase").empty().html(data);
		}
	});
}




//--------------------------------------------------------------------------------------------------------
//健康档案添加页面==药物过敏史调用
$("#drug_allergic").click(function(){
	var $text3 = $(this).text();
	if($text3 == "保存"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
		insertOrupdateAllergic("show");
	}
	if($text3 == "编辑"){
		getAllergicUpdatePage("update");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
	}
});
//健康档案查看页面==药物过敏史调用
$("#see_drug_allergic").click(function(){
	var $text4 = $(this).text();
	if($text4 == "保存"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
		insertOrupdateAllergic("show");
	}
	if($text4 == "编辑"){
		getAllergicUpdatePage("update");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
	}
});


function getAllergicUpdatePage(flag){
	$.ajax({
		type : "get",
		dataType: "html",
		url : "../allergic/updateAllergicSwitch",
		async: true,
		data:{
			memberId:$("#memberId").val(),
			flag:"show"
		},
		success : function(data) {
			$("#drugAllergy").empty().html(data);
		}
	});
}

function insertOrupdateAllergic(flag){
	if(validateDrugFlag==0){//如果不为0则不进行重复验证
		if(!validateDrug()){
			return;
		}
	}
	
	var memberId=$("#memberId").val();
	var lastUpdateNoDrugAllergic=$("#lastUpdateNoDrugAllergic").val();
	var healthDrugAllergyState=$("#healthDrugAllergyState").val();
	var serverStationCode=$("#serverStationCode").val();
	
	var antibioticArray=new Array;//存放相应type的drugName数组
	var sulfaArray=new Array;//存放相应type的drugName数组
	var calmsArray=new Array;//存放相应type的drugName数组
	var antipyreticmArray=new Array;//存放相应type的drugName数组
	var anesthesiaArray=new Array;//存放相应type的drugName数组
	var serumsArray=new Array;//存放相应type的drugName数组
	var pillsArray=new Array;//存放相应type的drugName数组
	var otherCheckArray=new Array;//存放其它过敏药物的数组
	
	
	//var otherCheck=$("#otherCheck").val();
	
	
	// <!--type: antibiotic抗生| sulfa磺胺 | calms镇定 |antipyreticm 退热 |anesthesia 麻醉|serums血清 |pills丸药|other 其它  -->
	$("input[data-type='antibiotic']:checked").each(function(index){
		antibioticArray.push(this.value);
	});
	$("input[data-type='sulfa']:checked").each(function(index){
		sulfaArray.push(this.value);
	});
	$("input[data-type='calms']:checked").each(function(index){
		calmsArray.push(this.value);
	});
	$("input[data-type='antipyreticm']:checked").each(function(index){
		antipyreticmArray.push(this.value);
	});
	$("input[data-type='anesthesia']:checked").each(function(index){
		anesthesiaArray.push(this.value);
	});
	$("input[data-type='serums']:checked").each(function(index){
		serumsArray.push(this.value);
	});
	$("input[data-type='pills']:checked").each(function(index){
		pillsArray.push(this.value);
	});
	var flagDrug=true;
	$("input[name='otherCheckArray']").each(function(index){
		if(index>0){
			if(this.value==''){
				
				return;//退出
			}
			
		}
		otherCheckArray.push(this.value);
	});

//	if(antibioticArray.length==0&&sulfaArray.length==0&&calmsArray.length==0
//			&&antipyreticmArray.length==0&&anesthesiaArray.length==0
//			&&serumsArray.length==0&&pillsArray.length==0&&otherCheckArray[0].trim()==''){
//		if(validateDrugFlag==0){
//			getAllergicUpdatePage(flag);
//		}
//		
//		return true;
//	}
	var healthBaseState = $("#healthBaseState").val();
	$.ajax({
		type : "post",
		dataType: "json",
		traditional :true, //如果为false则不能正常提交数组
		url : "../allergic/insertOrupdateAllergic",
		async: false,
		
		data:{
			memberId:memberId,
			lastUpdateNo:lastUpdateNoDrugAllergic,
			antibioticArray:antibioticArray,
			sulfaArray:sulfaArray,
			calmsArray:calmsArray,
			antipyreticmArray:antipyreticmArray,
			anesthesiaArray:anesthesiaArray,
			serumsArray:serumsArray,
			pillsArray:pillsArray,
			otherCheckArray:otherCheckArray,
			healthDrugAllergyState:healthDrugAllergyState
			
		},
		success : function(data) {
			if(data.isSuccess){
				zhyAlert("保存成功");
				success(memberId,serverStationCode,1,healthBaseState);
			
			}else{
				zhyAlert("保存失败");
//				$("#see_drug_allergic").removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
//				$("#drug_allergic").removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
			}
		}
	});
	
}
function success(memberId,serverStationCode,healthDrugAllergyState,healthBaseState){
		var url="../healthFile/get_history_page?memberId="+memberId+
		"&serverStationCode="+serverStationCode+"&healthDrugAllergyState="+healthDrugAllergyState+"&healthBaseState="+healthBaseState;
		url=encodeURI(encodeURI(url));
		window.location.href=url;
}
//------------------------------------------------------------------------------------------------------------------------------

//健康档案添加页面==暴露史调用
$("#expose").click(function(){
	var $text5 = $(this).text();
	if($text5 == "保存"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
		insertOrupdateExpose("show");
		
	}
	if($text5 == "编辑"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
		getExposeUpdatePage("update");
	}
});
//健康档案添加页面==暴露史调用
$("#see_expose").click(function(){
	var $text6 = $(this).text();
	if($text6 == "保存"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
		insertOrupdateExpose("show");
		
	}
	if($text6 == "编辑"){
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
		getExposeUpdatePage("update");
	}
});

function getExposeUpdatePage(flag){
	$.ajax({
		type : "get",
		dataType: "html",
		url : "../expose/updateExposeSwitch",
		async: true,
		data:{
			memberId:$("#memberId").val(),
			flag:flag
		},
		success : function(data) {
			$("#healthExpose").empty().html(data);
		}
	});
}

function insertOrupdateExpose(flag){
	var memberId=$("#memberId").val();
	var exposeName=$("input[name='exposeName']:checked").val();
	var lastUpdateNoExpose=$("#lastUpdateNoExpose").val();
	var healthExposeId=$("#healthExposeId").val();
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../expose/insertOrupdateExpose",
		async: false,
		data:{
			memberId:memberId,
			healthExposeId:healthExposeId,
			exposeName:exposeName,
			lastUpdateNo:lastUpdateNoExpose
		},
		success : function(data) {
		}
	});
}

// -----------------------------------------------------------------------------------------------------------------------------------
//健康档案添加页面==生活环境调用
$("#living").click(function(){
	var $text7 = $(this).text();
	if($text7 == "保存"){
		insertOrupdateLiving("show");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
	}
	if($text7 == "编辑"){
		getLivingUpdatePage("update");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
	}
});
//健康档案添加页面==生活环境调用
$("#see_living").click(function(){
	var $text8 = $(this).text();
	if($text8 == "保存"){
		insertOrupdateLiving("show");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn_edit pull-right").empty().text("编辑");
	}
	if($text8 == "编辑"){
		getLivingUpdatePage("update");
		$(this).removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
	}
});

function getLivingUpdatePage(flag){
	$.ajax({
		type : "get",
		dataType: "html",
		url : "../living/updateLivingSwitch",
		async: true,
		data:{
			memberId:$("#memberId").val(),
			flag:flag
		},
		success : function(data) {
			$("#healthLiving").empty().html(data);
		}
	});
}

function insertOrupdateLiving(flag){
	var memberId=$("#memberId").val();
	var ventilator=$("input[name='ventilator']:checked").val();
	var fuelType=$("input[name='fuelType']:checked").val();
	var waterType=$("input[name='waterType']:checked").val();
	var toiletType=$("input[name='toiletType']:checked").val();
	var livestockPen=$("input[name='livestockPen']:checked").val();
	var lastUpdateNoLiving=$("#lastUpdateNoLiving").val();
	var healthLivingId=$("#healthLivingId").val();
	
	return $.ajax({
		type : "post",
		dataType: "json",
		url : "../living/insertOrupdateExpose",
		async: false,
		data:{
			memberId:memberId,
			healthLivingId:healthLivingId,
			ventilator:ventilator,
			fuelType:fuelType,
			waterType:waterType,
			toiletType:toiletType,
			livestockPen:livestockPen,
			lastUpdateNo:lastUpdateNoLiving
		},
		success : function(data) {
		}
	});
}


// ---------------------------------------------验证-----------start

//基本信息验证
function validateHealthBase(){
	if($("#exercise_num").val()!=''&&$("#exercise_num").val()!=null){//如果填写了锻炼次数
		$("input[name='exerciseTime']").addClass("validate[required]");
		$("input[name='exerciseTime']").attr("data-errormessage","请选择锻炼时间");
		
		$("input[name='exerciseType']").addClass("validate[required]");
		$("input[name='exerciseType']").attr("data-errormessage","请选择锻炼方式");
		
	}else{
		$("input[name='exerciseTime']").removeClass("validate[required]");
		$("input[name='exerciseTime']").removeAttr("data-errormessage");
		
		$("input[name='exerciseType']").removeClass("validate[required]");
		$("input[name='exerciseType']").removeAttr("data-errormessage");
		$(".form-validation-field-15formError").remove();
		$(".form-validation-field-10formError").remove();
	}
	
	if($("#exercise_time").val()!=null&&$("#exercise_time").val()!=''){//如果选择的锻炼时间则 锻炼次数和锻炼方式不能为空
		$("#exercise_num").addClass("validate[required,min[1],max[100]]");
		$("#exercise_num").removeAttr("data-errormessage");
		$("#exercise_num").attr("data-errormessage","请填写1-100之间的数字");
		
		$("input[name='exerciseType']").addClass("validate[required]");
		$("input[name='exerciseType']").attr("data-errormessage","请选择锻炼方式");
		
	}
	if($("#exercise_type").val()!=null&&$("#exercise_type").val()!=''){//如果选择的锻炼方式
		$("#exercise_num").addClass("validate[required,min[1],max[100]]");
		$("#exercise_num").removeAttr("data-errormessage");
		$("#exercise_num").attr("data-errormessage","请填写1-100之间的数字");
		
		$("input[name='exerciseTime']").addClass("validate[required]");
		$("input[name='exerciseTime']").attr("data-errormessage","请选择锻炼时间");
		
	}
	var test=$("#healthBaseForm").validationEngine("validate");
	if(test){
		return true;
	} else{
		$(".undefinedformError").remove();
//		$("#health_base").removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
//		$("#see_member_base").removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
		return false;
	}
}
//验证药物过敏史
function validateDrug(){
//	if(!$("#allergic_form_add").validationEngine("validate")){
//		$("#see_drug_allergic").removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
//		$("#drug_allergic").removeAttr("class").attr("class","btn  btn-sm btn-save pull-right").empty().text("保存");
//		return false;
//	}else{
//		return true;
//	}
	$("#allergic_form_add").find(":text").blur();
//	console.log("len====>"+$(".formError").length);
	if($(".formError").length==0){
		return true;
	}else{
		return false;
	}
}
// ---------------------------------------------验证-----------end

function submitAll(){
	var flag=true;
	if(validateHealthBase()&&validateDrug()){//验证
		if($("#health_base").text()=='保存' ){
			validateBaseFlag=1;//如果为1则不进行重复验证，页面跳转
			save_health_base("修改页面获得查看页面");
		}
		if($("#see_member_base").text()=='保存' ){
			validateBaseFlag=1;//如果为1则不进行重复验证，页面跳转
			save_health_base("修改页面获得查看页面");
		}
		if($("#drug_allergic").text()=='保存'  ){
			validateDrugFlag=1;//如果为1则不进行重复验证，页面跳转
			insertOrupdateAllergic("show");
		}
		
		if($("#expose").text()=='保存'  ){
			validateExposeFlag=1;//如果为1则不进行页面跳转
			insertOrupdateExpose("show");
		}
		if($("#living").text()=='保存' ){
			validateLivingFlag=1;
			insertOrupdateLiving("show");
		}
		//跳转列表
		window.location="../healthFile/getIndexPage";
	}
}

function submitHealthBase(){
	var flag=true;
	var memberId = $("#memberId").val();
	if(validateHealthBase()){//验证
		//基本信息
		save_health_base("修改页面获得查看页面");
		//生活环境
		insertOrupdateLiving("show");
		//暴露史
		insertOrupdateExpose("show");
		window.location="../healthbase/getHealthBasePage?memberId="+memberId;//跳转到详情页
	}
}
function UpdateHealthBaseJsp(){
	var memberId = $("#memberId").val();
	var serverStationCode = $("#serverStationCode").val();
	window.location="../healthFile/get_insert_JSP?memberId="+memberId+"&serverStationCode="+serverStationCode;//跳转到编辑 
}

function UpdateHealthHistoryJsp(){
	var memberId = $("#memberId").val();
	var serverStationCode = $("#serverStationCode").val();
	var healthBaseState = $("#healthBaseState").val();
	success(memberId,serverStationCode,2,healthBaseState);
}
